/**
* @Title: warning_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('WarningAddListCtrl', ['$scope','FwaTabsetService','WarningAddService','$modal','BaseURL','Modal','WarningService',
	                                function($scope,FwaTabsetService,WarningAddService,$modal,baseUrl,Modal,WarningService){
		$scope.warningService = WarningService;
		WarningService.initMedicalOrgInfoList();
		$scope.form = {};
		$scope.form.warnDate = {};
		var currentPage = 1;
		var cellTemplate = 
			'<td>'+
				'<a  ng-click="clickLink(\'edit\',data)"">{{data.tranName}}</a>'+
			'</td>',
			cellTemplateView = 
				'<td>'+
				   '<a title=\'{{data.warnNoBiz}}\' ng-click="clickLink(\'warnQueryAdd.warningView\',data)"">{{data.warnNoBiz}}</a>'+
			    '</td>',
			indexNo = '<td>'+
				'{{$index+1}}'+
			'</td>';
       $scope.columns = [
        {label:'预警编号',width:'11%',cellClass:'text-left', cellTemplate:cellTemplateView},
        {label:'预警事件名称',map:'summary',width:'15%'},
        {label:'机构名称',map:'medicalOrgName',width:'18%'},
        {label:'处理方式',map:'process',width:'11%'},
        {label:'预警时段',map:'happenDate',width:'14%'},
       
        {label:'受理时间',map:'warnDate',width:'11%'},
        
        {label:'事件状态',map:'actName',width:'11%'},
        {label:'操作',width:'9%',cellClass:'text-left', cellTemplate:cellTemplate}

    ];
       $scope.clickLink = function (url,data){
    	   if(url==='edit'&&data.actNo==='N2014004'){
    		   FwaTabsetService.selectTab('warnQueryAdd.warningView',{warnNo:data.warnNo,actNo:'add',isBack:'Y'},false);
    	   }else if(url==='edit'){
    		   FwaTabsetService.selectTab('warnQueryAdd.warningEdit',{warnNo:data.warnNo,urlFlag:''},false);
    	   }else{
    		   FwaTabsetService.selectTab(url,{warnNo:data.warnNo,actNo:'add',isBack:'N'},false);
    	   }
    	   
       }
       $scope.addclickLink = function (){
    	   FwaTabsetService.selectTab('warnQueryAdd.warningAdd',null,false);
       }
		//每页显示数
        var start = 0,
            limit = 20;
        
//        $scope.form = {
//            page:"0",
//            pageSize:"20"
//        };

        
        var params = function (){
			var warnNoBiz=null;
			if($scope.form.warnNoBiz&&!angular.isUndefined($scope.form.warnNoBiz)){
				warnNoBiz = $scope.form.warnNoBiz;
			}
			var medicalOrgCode=null;
			if($scope.form.medicalOrgCode&&!angular.isUndefined($scope.form.medicalOrgCode)){
				medicalOrgCode = $scope.form.medicalOrgCode;
			}
			var summary=null;
			if($scope.form.summary&&!angular.isUndefined($scope.form.summary)){
				summary = $scope.form.summary;
			}
			var feedbackEndDate=null;
			if($scope.form.feedbackEndDate&&!angular.isUndefined($scope.form.feedbackEndDate)){
				feedbackEndDate = $scope.form.feedbackEndDate;
			}
//			var actNo=null;
//			if($scope.form.actNo&&!angular.isUndefined($scope.form.actNo)){
//				actNo = $scope.form.actNo;
//			}
			var startDate=null;
			if($scope.form.warnDate.start&&!angular.isUndefined($scope.form.warnDate.start)){
				startDate = $scope.form.warnDate.start;
			}
			var endDate=null;
			if($scope.form.warnDate.end&&!angular.isUndefined($scope.form.warnDate.end)){
				endDate = $scope.form.warnDate.end;
			}
			
			if(parseInt(currentPage) > 1){
                  start = (parseInt(currentPage) - 1) * limit;
            }
			var params = {
					warnNoBiz:warnNoBiz,
					medicalOrgCode:medicalOrgCode,
					summary:summary,
					feedbackEndDate:feedbackEndDate,
//					actNo:actNo,
					startDate:startDate,
					endDate:endDate,
					start:start,
					limit:limit
			}
			return params;
		}

		//分页点击查询
        $scope.$on('pageTool.skipPage',function(e,v){
            start = limit*(v-1);
            $scope.form.page = start;
            //$scope.onSubmit();
            query();
        });
        
        //查询
        function query(){
        	WarningService.warningAddQuery(params()).success(function(res){
                $scope.tableData = res.result;
                $scope.totalCount = res.total;
            });        	
        }

        //页面点击查询事件
        $scope.onSubmit = function(){
        	$scope.currentPage=1;
        	start =0;
        	 query();
        };
        //初始化加载数据
        $scope.onSubmit();
        
         //重置
        $scope.resetBut = function(){
			$scope.form = {};
			$scope.form.warnDate = {};
		}
        //---------------批量提交
       /* $scope.batchSubmitList=null;
        function getBatchSubmitData(){
        	var batchSubmitList=[];//选中的批量
        	var getSubmitList=[];//选中且状态不为撤销的批量
        	angular.forEach($scope.tableData,function(data){
        		if(data.checked){
            		batchSubmitList.push(data);
        		}
        	});
        	if(batchSubmitList.length>0){
        		for(var i=0;i<batchSubmitList.length;i++){
        			if(batchSubmitList[i].actNo==='N2014004'){//撤销
        				return false;
        			}else{
        				getSubmitList.push(batchSubmitList[i]);
        			}
        		}
        		
        		$scope.batchSubmitList=getSubmitList;
        	}
        }      */  
        //批量提交
        $scope.submitBatchWarning=function(){
        	 $scope.batchSubmitList=null;            
             	var batchSubmitList=[];//选中的批量
             	var getSubmitList=[];//选中且状态不为撤销的批量
             	var checkbatchData=true;
             	angular.forEach($scope.tableData,function(data){
             		if(data.checked){
                 		batchSubmitList.push(data);
             		}
             	});
             	if(batchSubmitList.length>0){
             		for(var i=0;i<batchSubmitList.length;i++){
             			if(batchSubmitList[i].actNo==='N2014004'){//撤销
             				checkbatchData=false;
             			}else{
             				getSubmitList.push(batchSubmitList[i]);
             			}
             		}             		
             		$scope.batchSubmitList=getSubmitList;
             	}              
        	if(checkbatchData===false){
        		Modal.alert('不能批量提交含有撤销的数据');
         		return;
        	}
        	if(!$scope.batchSubmitList){
        		 Modal.alert('请选择批量提交的数据！');
        		return;
        	}
        	var param = {					
					batchWarnList:$scope.batchSubmitList
			}	
        	Modal.confirm('确认批量提交吗？',function(){
        		$scope.loading=true;
				WarningAddService.addWarnBatchProcess(param).success(function(res){					
				  Modal.alert(res.msg);	
				  $scope.loading=false;				  
				  if(res.state=="0"){
					  $scope.onSubmit();
					}
	            });
			});			
        }
        //-------------批量结束
	}]);
})();